package cn.ekuma.epos.datalogic.define.dao;

import java.util.List;
import cn.ekuma.data.dao.BaseDAO;
import cn.ekuma.epos.db.table.I_Product;
import com.openbravo.bean.ProductReserve;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class ProductReserveDAO extends BaseDAO<ProductReserve>{

	public ProductReserveDAO(I_Session s) {
		super(s);
	}

	@Override
	public ProductReserve readValues(DataRead dr,ProductReserve product) throws BasicException {
		if(product==null)
			product=new ProductReserve();
		product.setID(dr.getString(1));
        product.setCode(dr.getString(2));
        product.setName( dr.getString(3));
        product.setCom(dr.getBoolean(4).booleanValue());
        product.setScale( dr.getBoolean(5).booleanValue());
        product.setM_origin(dr.getString(6));
        product.setM_quantity(dr.getDouble(7));
        product.setM_unit(dr.getString(8));
        product.setM_sManufacturer(dr.getString(9));
        product.setM_sSimilarCode(dr.getString(10));
        product.setM_iDurability(dr.getInt(11));
        product.setMnemonic(dr.getString(12));
        return product;
	}

	@Override
	public TableDefinition getTable() {
		 return new TableDefinition(s,
                 "PRODUCT_RESERVE",
            new Field[]{
              new Field(I_Product.ID, Datas.STRING, Formats.STRING),
              new Field(I_Product.CODE, Datas.STRING, Formats.STRING, false, true, true),
              new Field(I_Product.NAME, Datas.STRING, Formats.STRING, true, true, true),
              new Field(I_Product.ISCOM, Datas.BOOLEAN, Formats.BOOLEAN),
              new Field(I_Product.ISSCALE, Datas.BOOLEAN, Formats.BOOLEAN),
              new Field(I_Product.ORIGIN, Datas.STRING, Formats.STRING),
              new Field(I_Product.QUANTITY, Datas.DOUBLE, Formats.DOUBLE),
              new Field(I_Product.UNIT, Datas.STRING, Formats.STRING),
              new Field(I_Product.MANUFACTUR, Datas.STRING, Formats.STRING),
              new Field(I_Product.SIMILARCODE, Datas.STRING, Formats.STRING),
              new Field(I_Product.DURABILITY, Datas.INT, Formats.INT),
              new Field(I_Product.MNEMONIC, Datas.STRING, Formats.STRING)
              },
              new int[]{0}
            );
	}

	@Override
	public void writeInsertValues(DataWrite dp, ProductReserve obj)
			throws BasicException {
		dp.setString(1,obj.getID());
        dp.setString(2,obj.getCode());
        dp.setString(3, obj.getName());
        dp.setBoolean(4,obj.isCom());
        dp.setBoolean(5, obj.isScale());
        dp.setString(6,obj.getM_origin());
        dp.setDouble(7, obj.getM_quantity());
        dp.setString(8,obj.getM_unit());
        dp.setString(9,obj.getM_sManufacturer());
        dp.setString(10,obj.getM_sSimilarCode());
        dp.setInt(11,obj.getM_iDurability());
        dp.setString(12,obj.getMnemonic());
	}

	@Override
	public Class getSuportClass() {
		return ProductReserve.class;
	}

    public List<ProductReserve> listProductReserveBySimilarCode(String code) throws BasicException {
        return new PreparedSentence(s, this.getTableDefinition().getListSQL()+" WHERE SIMILARCODE=?", SerializerWriteString.INSTANCE, this).list(code);
    }

    public final ProductReserve getProductReserveByCode(final String sCode) throws BasicException {
        return (ProductReserve) new PreparedSentence(s, getTableDefinition().getListSQL()+" WHERE CODE=? ", SerializerWriteString.INSTANCE, this).find(sCode);
    }
}
